function getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	return new Array(pageWidth,pageHeight,windowWidth,windowHeight);
};
/**
 / THIRD FUNCTION
 *
 * @return Array Return an array with x,y page scroll values.
 */
function getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	return new Array(xScroll,yScroll);
};

var $jQ = jQuery.noConflict();

//getMouseXY
var _mousex = 0, _mousey = 0;
function getMouseXY(e) {var cursor = {x:_mousex,y:_mousey}; e = e||window.event; if (! (e && e.clientX) ) return cursor; if (e.pageX || e.pageY) { cursor.x = e.pageX; cursor.y = e.pageY; } else { var de = document.documentElement; var b = document.body; cursor.x = e.clientX + (de.scrollLeft || b.scrollLeft) - (b.clientLeft || 0); cursor.y = e.clientY + (de.scrollTop || b.scrollTop) - (b.clientTop || 0); } return cursor;}
function setMouseXY(x,y) { _mousex = x; _mousey = y; }

function thLightBox(u,p,o) {
	var options = $jQ.extend({
		width			:	910,
		height			:	0,
		title			:	'',
		noContainer		:	true,
		autoHash		:	false,
		imageLoading	:	'/images/lightbox/ico-loading.gif',
		imageBtnPrev	:	'/images/lightbox/btn-prev.gif',
		imageBtnNext	:	'/images/lightbox/btn-next.gif',
		imageBtnClose	:	'/images/lightbox/btn-close.gif',
		prefixURL		: ""
	},o);
	
	var _nolbstr = '<div id="jquery-overlay"></div><div id="jquery-lightbox" style="top:0px;left:0px;"><a href="#" id="lightbox-secNav-btnClose"><div class="windowClose"> </div></a><div id="lightboxContent"><div id="lightbox-loading"><img src="' + options.imageLoading + '"></div></div></div>';
	var _lbstr = '<div id="jquery-overlay"></div><div id="jquery-lightbox" style="top:0px;left:0px;"><div id="lightbox-container-image-data-box"><div id="lightbox-container-image-data"><div id="lightbox-image-details"><span id="lightbox-image-details-caption"></span></div><div id="lightbox-secNav"><a href="#" id="lightbox-secNav-btnClose"><img src="/images/lightbox/btn-close.gif"></a></div></div></div><div id="lightbox-container-image-box"><div id="lightbox-container-image"><div id="lightboxContent"><div id="lightbox-loading"><img src="' + options.imageLoading + '"></div></div></div></div></div>';
	
	_vbclose();
	_ghtml( options.prefixURL + u );

	function _ghtml(u) {
		$jQ('body').append( options.noContainer ? _nolbstr : _lbstr );
		var psize = getPageSize();
		var pscroll = getPageScroll();
		
		if ( options.width < 1 ) options.width = psize[2] - 200;
		if ( options.height < 1 ) options.height = psize[3] - 100;

		$jQ('#jquery-overlay').css({
			backgroundColor	:	'#000000',
			opacity			:	0.8,
			width			:	psize[0],
			height			:	psize[1]
		}).fadeIn();
		
		$jQ('#jquery-lightbox').css({
			top: pscroll[1] + 10,
			left:	pscroll[0] + ( psize[2] - options.width-20 ) /2,
			width: options.width+20
		}).show();
		
		$jQ('#lightbox-secNav-btnClose').bind('click', _vbclose);
		
		$jQ.get(u,p,function(data) {
			_lbox(data,options.title);
			thAjaxComplete(u,options);
		});
	}
	
	function _lbox(c,t) {
		if ( ! options.noContainer ) {
			$jQ('#lightbox-image-details-caption').html(t);
			$jQ('#lightbox-container-image-data-box').css({width: options.width});
			$jQ('#lightbox-container-image-box').css({width: options.width+20, height: options.height+20});
		}
		
		$jQ('#lightboxContent').html(c);
	}
	function _vbclose() {
		var jo = $jQ('#jquery-overlay');
		if (jo) { 
			$jQ('#jquery-lightbox').remove();
			jo.remove();//jo.fadeOut(function() { jo.remove(); });
		}
		return false;
	}
};

(function ($) {	
	//thSlide function
	$.fn.thSlide= function(options) {
		var options = $.extend({
			slideSelector: '.slides',
			slideWidth		: 300,
			slideHeight		: 0,
			thumbSelector: '.thumbs',
			thumbItemSelector: 'li',
			slideItemClass: 'slideItem',
			thumbItemClass: 'thumbItem',
			time: 300,
			auto: false,
			autoSecond: 3
		}, options);
		
		this.each(function () {
			var $o = $(this);
			var $so = $o.find(options.slideSelector);
			var $to = $o.find(options.thumbSelector);
			
			var sw=0;
			var positions = new Array();
			$so.find( 'img' ).each(function(i){
				positions[i]= sw;
				var iw = (options.slideHeight > 0) ? options.slideHeight : options.slideWidth;//parseInt ( $(this).attr('width') );
				sw += iw;
			});
			if (options.slideHeight > 0)
				$so.height(sw);
			else
				$so.width(sw);

			/* On a thumbnail click */
			$to.find(options.thumbItemSelector + '.' + options.thumbItemClass + ' a').click(function(e){
				var ao = $(this);
				$( options.thumbItemSelector + '.' + options.thumbItemClass ).removeClass('active').addClass('inactive');
				ao.parent().addClass('active');
				
				var pos = ao.parent().prevAll('.' + options.thumbItemClass).length;
				
				if (options.slideHeight > 0)
					$so.stop().animate({marginTop:-positions[pos]+'px'}, options.time );
				else
					$so.stop().animate({marginLeft:-positions[pos]+'px'}, options.time );
				
				e.preventDefault();
				return false;
			});
	
			$to.find(options.thumbItemSelector + '.' + options.thumbItemClass + ':first').addClass('active').siblings().addClass('inactive');

			/* auto slide */
			if ( options.auto ) {
				var current = 1;
				var toa = $to.find(options.thumbItemSelector + '.' + options.thumbItemClass + ' a');
				setInterval( function(){
					toa.eq( current % toa.length).trigger('click',[true]);
					current++;
				}, options.autoSecond * 1000);
			}
		});
		
		return this;
	};//end thSlide
	
	//thZoom
	$.fn.thZoom = function(options){
        options = $.extend({
			'diameter':200,
			'borderColor':'#ccc',
			'borderWidth':'1px',
			'borderStyle':'solid',
			marginLeft: 100,
			marginTop: 0,
			time: 300
		}, options);

		this.each(function() {
			var $o = $(this);
			var loupe = $o;
			var wrpImage; 
			var lWidth; 
			var lHeight; 
			var sWidth; 
			var sHeight;
			
			var oimage = new Image();
			oimage.src = $o.attr('fullsrc');
			
			$o.bind({
				mouseenter: function(e) {
					$o.css('cursor','crosshair'); 
					sWidth = $o.width(); 
					sHeight = $o.height(); 

					var lImage = oimage;
					lWidth = lImage.width;
					lHeight = lImage.height;
					
					loupe = $('<div>').css({
						position: 'absolute',
						width: options.diameter,
						height: options.diameter,
						'border-width': options.borderWidth,
						'border-style':  options.borderStyle,
						'border-color': options.borderColor,
						overflow: 'hidden',
						'z-index': '2000',
						/*
						'-moz-border-radius': options.diameter/2,
						'-khtml-border-radius': options.diameter/2,
						'border-radius': options.diameter/2,
						*/
						background: 'url('+lImage.src+') no-repeat'
					}).attr('id','iLoupe');				
						
					$('body').append(loupe);
				},
				mousemove: function(e) {
					var sxPointer = e.pageX - $o.offset().left - parseInt($o.css('padding-left'));
					var syPointer = e.pageY - $o.offset().top - parseInt($o.css('padding-top'));
					
					loupe.css({
						left: e.pageX + options.marginLeft,
						top: e.pageY + options.marginTop
					});
					
					var sxPointerPer = sxPointer/sWidth*100;
					var syPointerPer = syPointer/sHeight*100;
					sxPointerPer = Math.round(sxPointerPer*Math.pow(10,10))/Math.pow(10,10);
					syPointerPer = Math.round(syPointerPer*Math.pow(10,10))/Math.pow(10,10);
					
					var sXPointer = Math.round(((sxPointerPer/100*lWidth)*Math.pow(10,0))/Math.pow(10,0));
					var sYPointer = Math.round(((syPointerPer/100*lHeight)*Math.pow(10,0))/Math.pow(10,0));
					
					(sxPointer>=0 && sxPointer<=sWidth && syPointer>=0 && syPointer<=sHeight) ? loupe.css({"background-position":(-sXPointer+options.diameter/2)+"px "+(-sYPointer+options.diameter/2)+"px"}) : false;
				},
				mouseleave: function() {
					loupe.stop(true, true).fadeOut(options.time, function() {
						loupe.remove();
					});
				}             
			});
		});
		
        return this;
	};//ends thZoom
})(jQuery);

// public functions
var PI = Math.PI;

/* Discard the fractional part of a number, e.g., INT(3.2) = 3 */
function INT(d) {
	return Math.floor(d);
}

